{include file="header.html"}
<script src="/js/ckeditor/ckeditor.js"></script>
<style>
.input1{ float:right; margin:-7px 0px 0px -30px; display:inline-block; width:65%; }
</style>
<div class="page-header">
	<h6>
		优惠劵管理
		<small>
			<i class="ace-icon fa fa-angle-double-right"></i>
			优惠劵添加
		</small>
	</h6>
</div>
<div class="row">
<div class="col-xs-12">
	<form class="form-horizontal form" role="form" method="post" action="?app=coupon&act=add" enctype="multipart/form-data">
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="name"> 优惠劵名称 </label>
			<div class="col-sm-10">
				<input type="text" id="name" name="name" value="" class="col-xs-4" />
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="coupon_money"> 面值 </label>
			<div class="col-sm-10">
				<input type="text" id="coupon_money" name="coupon_money" value="" class="col-xs-1" style="margin-right:10px;" />
				<div class="checkbox">
					<label>
						<input type="checkbox" name="all_price" id="all_price" value="1" class="ace" />
						<span class="lbl"> 全额 </span>
					</label>
				</div>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="status"> 使用条件 </label>
			<div class="col-sm-10">
				<div class="radio">
					<label>
						<input type="radio" name="min" value="1" checked class="ace price2" />
						<span class="lbl" style="display:inline-block;"> 満
							<input type="text" id="min_price" name="min_price" value="" class="col-xs-2 input1" placeholder="金额" />
						</span>
					</label>
				</div>
				<div class="radio">
					<label>
						<input type="radio" name="min" value="0" class="ace price" />
						<span class="lbl"> 无条件 </span>
					</label>
				</div>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="begin_time"> 有效时间 </label>
			<div class="col-sm-10">
				<input type="text" id="begin_time" name="begin_time" value="" placeholder="留空为无开始时间" class="col-xs-2" />
				<span class="some-line">-</span>
				<input type="text" id="end_time" name="end_time" value="" placeholder="留空为无限期" class="col-xs-2" style="margin-right:10px;" />
				<div class="checkbox">
					<label>
						<input type="checkbox" id="handy_time_handle" class="ace" />
						<span class="lbl"> 动态期限 </span>
					</label>
				</div>
				<input type="text" id="handy_time" name="handy_time" value="" class="col-xs-1" placeholder="天数" style="display:none;width:44px;" />
				<span class="some-line" style="display:none;">获取后该天数内有效</span>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="quantity"> 发行量 </label>
			<div class="col-sm-10">
				<input type="text" id="quantity" name="quantity" value="" class="col-xs-1" />
				<span class="some-line">张</span>
				<div class="checkbox">
					<label>
						<input type="checkbox" name="auto_add" id="auto_add" value="1" class="ace" />
						<span class="lbl"> 自动增加 </span>
					</label>
				</div>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="num_per_person"> 每人限领 </label>
			<div class="col-sm-10">
				<input type="text" id="num_per_person" name="num_per_person" value="" class="col-xs-1" />
				<span class="some-line">张</span>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="times"> 可用次数 </label>
			<div class="col-sm-10">
				<input type="text" id="times" name="times" value="1" class="col-xs-1" placeholder="次数" style="margin-right:10px;" />
				<div class="checkbox">
					<label>
						<input type="checkbox" name="unlimited" id="unlimited" value="1" class="ace" />
						<span class="lbl"> 无限次 </span>
					</label>
				</div>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="day_times"> 一天内可用次数 </label>
			<div class="col-sm-10">
				<input type="text" id="day_times" name="day_times" value="1" class="col-xs-1" placeholder="次数" style="margin-right:10px;" />
				<div class="checkbox">
					<label>
						<input type="checkbox" name="day_times_handle" id="day_times_handle" value="1" class="ace" />
						<span class="lbl"> 不限次 </span>
					</label>
				</div>
			</div>
		</div>
		<!--
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="type"> 类型 </label>
			<div class="col-sm-10">
				<select name="type" id="type">
					<option value="0">普通</option>
					<option value="1">品牌</option>
					<option value="2">项目</option>
					<option value="3">新人</option>
				</select>
			</div>
		</div>
		-->
		<!--
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="offline_use"> 使用环境 </label>
			<div class="col-sm-10">
				<div class="radio">
					<label>
						<input type="radio" name="offline_use" value="1" checked class="ace" />
						<span class="lbl"> 线下 </span>
					</label>
				</div>
				<div class="radio">
					<label>
						<input type="radio" name="offline_use" value="0" class="ace" />
						<span class="lbl"> 线上线下通用 </span>
					</label>
				</div>
			</div>
		</div>
		-->
		<!--
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="postion"> 使用位置 </label>
			<div class="col-sm-10">
				<select name="postion" id="postion">
					<option value="1">通用</option>
					<option value="2">app专享</option>
					<option value="3">微信专享</option>
					<option value="4">app+微信专享</option>
				</select>
			</div>
		</div>
		-->
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" for="permit_goods"> 是否限制使用范围 </label>
			<div class="col-sm-10">
				<select name="permit_goods" id="permit_goods">
					<option value="0">不限制</option>
					<option value="1">限制</option>
				</select>
			</div>
		</div>
		<div class="form-group goods-view" style="display:none;">
			<label class="col-sm-2 control-label no-padding-right"><span class="btn-info btn-add" id="add">+</span> 选择可使用的{if $coupon_permit_category==0}商品{else}分类{/if}</label>
			<div class="col-sm-10">
				<ul id="simple-table" class="goods-list col-sm-5"></ul>
			</div>
		</div>
		<div class="form-group">
			<label class="col-sm-2 control-label no-padding-right" style="text-align:right;" for="status"> 状态 </label>
			<div class="col-sm-10">
				<div class="radio">
					<label>
						<input type="radio" name="status" value="1" checked class="ace" />
						<span class="lbl"> 正常 </span>
					</label>
				</div>
				<div class="radio">
					<label>
						<input type="radio" name="status" value="0" class="ace" />
						<span class="lbl"> 隐藏 </span>
					</label>
				</div>
				<div class="radio">
					<label>
						<input type="radio" name="status" value="-2" class="ace" />
						<span class="lbl"> 过期 </span>
					</label>
				</div>
			</div>
		</div>

		<div class="clearfix form-actions">
			<div class="col-md-offset-3 col-md-9">
				<button class="btn btn-info" type="submit">
					<i class="ace-icon fa fa-check bigger-110"></i>
					提交
				</button>

				&nbsp; &nbsp; &nbsp;
				<button class="btn" type="reset">
					<i class="ace-icon fa fa-undo bigger-110"></i>
					重置
				</button>
			</div>
		</div>
	</form>
</div>
</div>
<script>
$(function(){
	$('#simple-table').on('click', 'a.del', function(){
		$(this).parent().remove();
	});
	$('#permit_goods').change(function(){
		if($(this).selected().val()==0){
			$('.goods-view').hide();
		}else{
			$('.goods-view').show();
		}
	});
	$('#begin_time').datepicker();
	$('#end_time').datepicker();
	$('.price').click(function(){
		$('.input1').hide();
		$('.input1').val('');
	});
	$('.price2').click(function(){
		$('.input1').show();
	});
	$('#all_price').change(function(){
		if(this.checked)$('#coupon_money').hide();
		else $('#coupon_money').show();
	});
	$('#unlimited').change(function(){
		if(this.checked)$('#times').hide();
		else $('#times').show();
	});
	$('#handy_time_handle').change(function(){
		var parent = $(this).parents('.checkbox').eq(0);
		if(this.checked){
			parent.prev().hide().prev().hide().prev().hide();
			parent.next().show().next().show();
		}else{
			parent.prev().show().prev().show().prev().show();
			parent.next().hide().next().hide();
		}
	});
	$('#auto_add').change(function(){
		var parent = $(this).parents('.checkbox').eq(0);
		if(this.checked){
			parent.prev().hide().prev().hide().prev().hide();
		}else{
			parent.prev().show().prev().show().prev().show();
		}
	});
	$('#day_times_handle').change(function(){
		if(this.checked)$('#day_times').hide();
		else $('#day_times').show();
	});
    $('#add').click(function(){
		var style = '', win = $.window();
		if(win.width<=768)style = 'style="width:'+win.width+'px;height:'+win.height+'px;"';
		var html = '<div class="goods" '+style+'>\
			<div class="goods-title"><a href="javascript:void(0)">×</a>搜索{if $coupon_permit_category==0}商品{else}分类{/if}</div>\
			<input type="text" class="keywords" placeholder="输入{if $coupon_permit_category==0}商品{else}分类{/if}关键字后回车进行搜索" />\
			<ul class="goods-list"></ul>\
		</div>';
        $.overlay(html, 0, null, function(){
			$('.goods .goods-list').stopScroll(false);
		});
		$('.goods .goods-title a').click(function(){
			$.overlay(false);
		});
		$('.goods .goods-list').stopScroll(true);
		$('.goods .keywords').onkey({
			callback : function(code){
				if(code==13){
					var _this = $(this);
					if(!_this.val().length){ alert('请输入关键字');return }
					$.getJSON('api.php?app={if $coupon_permit_category==0}goods{else}category&all=1{/if}&act=index&keyword='+_this.val(), function(json){
						if(json.error!=0){ alert('请输入关键字');return }
						if(!$.isArray(json.data.rs)){ alert('搜索不到相关{if $coupon_permit_category==0}商品{else}分类{/if}');return }
						var html = '';
						for(var i=0; i<json.data.rs.length; i++){
							{if $coupon_permit_category==0}
							html += '<li class="item">\
								<a href="javascript:void(0)">\
									<div style="background-image:url('+json.data.rs[i].pic+');"></div>\
									<span>'+json.data.rs[i].name+'</span>\
									<font>￥'+json.data.rs[i].price+'</font>\
								</a>\
							</li>';
							{else}
							var name = [];
							$.each(json.data.rs[i].parents, function(){
								name.push(this.name);
							});
							html += '<li class="row">\
								<a href="javascript:void(0)">'+name.join('<em>＞</em>')+'</a>\
							</li>';
							{/if}
						}
						$('.goods .goods-list').html(html);
						$('.goods .goods-list a').each(function(i){
							$(this).data('data', json.data.rs[i]).click(function(){
								//$.overlay(false);
								var data = $(this).data('data'), hasGoods = false;
								$('.goods_id').each(function(){
									if(Number($(this).val())==Number(data.id)){
										hasGoods = true;
										return false;
									}
								});
								if(hasGoods)return;
								{if $coupon_permit_category==0}
								var html = '<li class="item">\
									<a class="del" href="javascript:void(0)">－</a>\
									<a href="index.php?app=goods&act=edit&id='+data.id+'" target="_blank">\
										<div style="background-image:url('+data.pic+');"></div>\
										<span>'+data.name+'</span>\
										<font>￥'+data.price+'</font>\
									</a>\
									<input type="hidden" class="goods_id" name="goods[]" value="'+data.id+'" />\
								</li>';
								{else}
								var name = [];
								$.each(data.parents, function(){
									name.push(this.name);
								});
								var html = '<li class="row">\
									<a class="del" href="javascript:void(0)">－</a>\
									<a href="index.php?app=category&act=edit&id='+data.id+'" target="_blank">'+name.join('<em>＞</em>')+'</a>\
									<input type="hidden" class="goods_id" name="goods[]" value="'+data.id+'" />\
								</li>';
								{/if}
								$('#simple-table').append(html);
							});
						});
					});
				}
			}
		});
    });
});
</script>
{include file="footer.html"}